package com.cnbot.xfmic;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.cnbot.xfsixmicserialport.api.WakeUpTools;

public class MainActivity extends AppCompatActivity implements WakeUpTools.IWakeUpListener {
    private final String TAG = MainActivity.class.getSimpleName();
    private WakeUpTools wakeUpTools;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        wakeUpTools = WakeUpTools.getInstance();
        boolean open = wakeUpTools.openWakeSerialPort(115200, "/dev/ttyUSB0");
        Log.e(TAG, "串口打开状态 = " +open);
        if (open) {
            wakeUpTools.startWakeUp();
            wakeUpTools.setiWakeUpListener(this);
        }

        test();
    }

    private void test(){
        String c = " \n" +
                "    \n" +
                "    \n" +
                "    WAKE UP!angle:112 score:1864  key_word: xiao3".trim();

        String info[] = c.split("\n");
        if (info != null && info.length > 0) {
            String angle = info[info.length -1];
            Log.e(TAG, "content=" + angle);
            if (angle.contains("angle:")) {
                String angleValue = angle.split(" ")[1];
                String an[] =angleValue.split(":");
                String v = an[1];
                Log.e(TAG, "angle = " +v);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (wakeUpTools != null) {
            wakeUpTools.removeiWakeUpListener();
            wakeUpTools.stopWakeUp();
        }
    }

    @Override
    public void onWakeUp(final String angle) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG, "唤醒角度 = " + angle);
                Toast.makeText(MainActivity.this, "唤醒角度 = " + angle, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
